package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcLifeActionOldmanMapper;
import com.csun.cmny.provider.dto.OmcLifeActionOldmanDto;
import com.csun.cmny.provider.dto.OmcLifeOldmanQueryDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcLifeActionOldman;
import com.csun.cmny.provider.model.domain.OmcLifeOldmanRoom;
import com.csun.cmny.provider.model.dto.OldmanCheckStatDto;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.service.DmcRpcService;
import com.csun.cmny.provider.service.OldmanService;
import com.csun.cmny.provider.service.OmcLifeActionOldmanService;
import com.csun.cmny.provider.service.OmcLifeOldmanRoomService;
import com.csun.cmny.provider.vo.OmcLifeOldmanVo;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Lijun
 * @description:
 * @date 2019/12/31 9:56
 */
@Slf4j
@Service
@Transactional
public class OmcLifeActionOldmanServiceImpl extends BaseService<OmcLifeActionOldman> implements OmcLifeActionOldmanService {

    @Resource
    private OmcLifeActionOldmanMapper omcLifeActionOldmanMapper;

    @Resource
    private OldmanService oldmanService;

    @Resource
    private OmcLifeOldmanRoomService omcLifeOldmanRoomService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 添加入居者
     * @param omcLifeActionOldmanDto
     * @param loginAuthDto
     * @return
     */
    @Override
    public Integer addLifeOldman(OmcLifeActionOldmanDto omcLifeActionOldmanDto, LoginAuthDto loginAuthDto){
        //校验老人编号是否重复
        Oldman oldman2 = new Oldman();
        oldman2.setOldmanNo(omcLifeActionOldmanDto.getOldmanNo());
        oldman2.setHomeId(loginAuthDto.getGroupId().intValue());
        List<Oldman> oldman1 = oldmanService.selectByNo(oldman2);
        if (oldman1.size()>0) {
            throw new OmcBizException(ErrorCodeEnum.DMC100615002);
        }

        //添加老人表数据
        Oldman oldman = new Oldman();
        oldman.setOldName(omcLifeActionOldmanDto.getOldSurname()+omcLifeActionOldmanDto.getOldFirstName());
        oldman.setOldmanNo(omcLifeActionOldmanDto.getOldmanNo());
        oldman.setEgPhonea(omcLifeActionOldmanDto.getEgPhonea());
        oldman.setEgPhoneb(omcLifeActionOldmanDto.getEgPhoneb());
        oldman.setHomeId(loginAuthDto.getGroupId().intValue());
        int ret = oldmanService.insertSelective(oldman);
        if (ret<1){
            throw new OmcBizException(ErrorCodeEnum.GL99990500);
        }
        //添加LifE入居者老人补充表数据
        OmcLifeActionOldman omcLifeActionOldman = new OmcLifeActionOldman();
        BeanUtils.copyProperties(omcLifeActionOldmanDto,omcLifeActionOldman);
        omcLifeActionOldman.setOldmanId(oldman.getOldmanId().longValue());
        omcLifeActionOldman.setGroupId(loginAuthDto.getGroupId());

        /**
         * @author  chisj
         * @date    2020-11-18
         * @desc    新增入住者状态
         */


        //添加房间老人关联表
        OmcLifeOldmanRoom omcLifeOldmanRoom = new OmcLifeOldmanRoom();
        omcLifeOldmanRoom.setOldmanId(oldman.getOldmanId().longValue());
        omcLifeOldmanRoom.setRoomId(omcLifeActionOldmanDto.getRoomId());
        omcLifeOldmanRoom.setCheckInStatus(1);
        Date date = new Date();
        omcLifeOldmanRoom.setModifytime(date);
        omcLifeOldmanRoom.setModifier(loginAuthDto.getUserId());
        omcLifeOldmanRoom.setCreatetime(date);
        ret = omcLifeOldmanRoomService.save(omcLifeOldmanRoom);
        if (ret<1){
           throw new OmcBizException(ErrorCodeEnum.GL99990500);
        }
        ret =  save(omcLifeActionOldman);
        if (ret<1){
            throw new OmcBizException(ErrorCodeEnum.GL99990500);
        }
        return ret;
    }

    /**
     * 删除入居者
     * @param oldmanId
     * @return
     */
    @Override
    public Integer deleteById(Long oldmanId){

        // 根据老人Id查询roomId
        Long roomId = omcLifeOldmanRoomService.queryRoomIdByOldmanId(oldmanId);

        //将老人房间关联表中的数据删除
        OmcLifeOldmanRoom omcLifeOldmanRoom = new OmcLifeOldmanRoom();
        omcLifeOldmanRoom.setOldmanId(oldmanId);
        int ret = omcLifeOldmanRoomService.delete(omcLifeOldmanRoom);
        if (ret<1){
            throw new OmcBizException(ErrorCodeEnum.GL99990500);
        }
        //将老人表数据删除
        ret = oldmanService.deleteByOldId(oldmanId.intValue());
        if (ret<1){
            throw new OmcBizException(ErrorCodeEnum.GL99990500);
        }
        //将入居者补充表数据删除
        ret = omcLifeActionOldmanMapper.deleteByPrimaryKey(oldmanId);
        if (ret<1){
            throw new OmcBizException(ErrorCodeEnum.GL99990500);
        }

        if (PublicUtil.isNotEmpty(roomId)) {
            // 删除
            redisTemplate.delete(RedisKeyUtil.getOmcRoomCheckStat(roomId));
        }


        return ret;
    }

    /**
     * 查询入居者
     * @param omcLifeOldmanQueryDto
     * @return
     */
    @Override
    public PageInfo queryLifeOldmanWithPage(OmcLifeOldmanQueryDto omcLifeOldmanQueryDto){
        PageHelper.startPage(omcLifeOldmanQueryDto.getPageNum(), omcLifeOldmanQueryDto.getPageSize());
        List<OmcLifeOldmanVo> list = omcLifeActionOldmanMapper.queryLifeOldmanWithPage(omcLifeOldmanQueryDto);
        return new PageInfo<>(list);
    }

    /**
     * 修改入居者
     * @param omcLifeActionOldmanDto
     * @param loginAuthDto
     * @return
     */
    @Override
    public Integer updateById(OmcLifeActionOldmanDto omcLifeActionOldmanDto,LoginAuthDto loginAuthDto){

        // 老人Id
        Long oldmanId = omcLifeActionOldmanDto.getOldmanId();
        // 房间Id
        Long roomId = omcLifeActionOldmanDto.getRoomId();

        //校验老人编号是否重复
        Oldman oldman1 = new Oldman();
        oldman1.setOldmanId(omcLifeActionOldmanDto.getOldmanId().intValue());
        Oldman oldman0 = oldmanService.selectByOld(oldman1);
        if(!omcLifeActionOldmanDto.getOldmanNo().equals(oldman0.getOldmanNo())){
            Oldman oldman2 = new Oldman();
            oldman2.setOldmanNo(omcLifeActionOldmanDto.getOldmanNo());
            oldman2.setHomeId(loginAuthDto.getGroupId().intValue());
            List<Oldman> list = oldmanService.selectByNo(oldman2);
            if (list.size() > 0) {
                throw new OmcBizException(ErrorCodeEnum.DMC100615002);
            }
        }

        // TODO 删除之前老人入住房间的入住状态

        //删除之前的老人绑定房间数据
        OmcLifeOldmanRoom omcLifeOldmanRoom = new OmcLifeOldmanRoom();
        omcLifeOldmanRoom.setOldmanId(omcLifeActionOldmanDto.getOldmanId());
        int ret = omcLifeOldmanRoomService.delete(omcLifeOldmanRoom);
        if (ret<1){
            throw new OmcBizException(ErrorCodeEnum.GL99990500);
        }
        Oldman oldman = new Oldman();
        oldman.setOldmanId(omcLifeActionOldmanDto.getOldmanId().intValue());
        oldman.setOldName(omcLifeActionOldmanDto.getOldSurname()+omcLifeActionOldmanDto.getOldFirstName());
        oldman.setOldmanNo(omcLifeActionOldmanDto.getOldmanNo());
        oldman.setEgPhonea(omcLifeActionOldmanDto.getEgPhonea());
        oldman.setEgPhoneb(omcLifeActionOldmanDto.getEgPhoneb());
        //保存老人表中修改后的入居者数据
        ret = oldmanService.updateByOldId(oldman);
        if (ret<1){
            throw new OmcBizException(ErrorCodeEnum.GL99990500);
        }
        OmcLifeActionOldman omcLifeActionOldman = new OmcLifeActionOldman();
        BeanUtils.copyProperties(omcLifeActionOldmanDto,omcLifeActionOldman);
        Date date = new Date();
        OmcLifeOldmanRoom omcLifeOldmanRoom1 = new OmcLifeOldmanRoom();
        omcLifeOldmanRoom1.setOldmanId(omcLifeActionOldmanDto.getOldmanId());
        omcLifeOldmanRoom1.setRoomId(omcLifeActionOldmanDto.getRoomId());
        omcLifeOldmanRoom1.setCheckInStatus(1);
        omcLifeOldmanRoom1.setModifytime(date);
        omcLifeOldmanRoom1.setModifier(loginAuthDto.getUserId());
        omcLifeOldmanRoom1.setCreatetime(date);
        ret = omcLifeOldmanRoomService.save(omcLifeOldmanRoom1);
        if (ret<1){
           throw new OmcBizException(ErrorCodeEnum.GL99990500);
        }


        /**
         * @author  chisj
         * @date    2020-11-18
         * @desc    判断是否有修改入住者状态
         */
        Integer checkStat = omcLifeActionOldmanMapper.queryCheckStatByOldmanId(oldmanId);
        Integer newCheckStat = omcLifeActionOldmanDto.getCheckStat();
        if (PublicUtil.isNotEmpty(newCheckStat)) { // 1-->1 || 2-->2 不做处理
            if (!newCheckStat.equals(checkStat)) {
                // 如果从1(入居中) --> 2(一时外出中) 不产生新的报警, 可以查出老的报警记录
                // 如果从2(一时外出中) --> 1(入居中) 可以产生报警
                OldmanCheckStatDto oldmanCheckStatDto = new OldmanCheckStatDto(oldmanId, roomId, newCheckStat);
                redisTemplate.opsForValue().set(RedisKeyUtil.getOmcRoomCheckStat(roomId), oldmanCheckStatDto);
            }
        }

        ret = omcLifeActionOldmanMapper.updateByPrimaryKey(omcLifeActionOldman);
        if (ret<1){
            throw new OmcBizException(ErrorCodeEnum.GL99990500);
        }
        return ret;
    }
}
